//
//  UserProfileView.swift
//  App_Profile
//
//  Created by 蒋晨成 on 2022/1/13.
//

import Adaptor
import App_Common_Views
import SnapKit
import UICore
import UIKit

protocol ProfilePersonalInfoViewDelegate: AnyObject {
    func infoViewDidSelected(_ infoView: ProfilePersonalInfoView)
}

class ProfilePersonalInfoView: View {
    private lazy var contentView: UIView = .init()
        .x
        .backgroundColor(Adaptor.Profile.background)
        .instance

    lazy var avatarView: AvatarView = {
        let avatar = AvatarView(action: nil, option: .init(cornerRadius: 40, border: false))
        avatar.set(url: nil, placeholder: ProfileModular.image(named: "Common.Avatar.Placeholder"))
        return avatar
    }()

    lazy var userNameLabel: UILabel = .init()
        .x
        .font(.boldSystemFont(ofSize: 32))
        .textColor(Adaptor.Profile.titleColor)
        .text(ProfileModular.localizedString(key: "Profile.Home.Info.Username.Placeholder"))
        .instance

    lazy var signatureLabel: UILabel = .init()
        .x
        .font(.systemFont(ofSize: 16))
        .textColor(Adaptor.Profile.subtitleColor)
        .text(ProfileModular.localizedString(key: "Profile.Home.Info.Signature.Placeholder"))
        .instance

    weak var delegate: ProfilePersonalInfoViewDelegate?

    override init(frame: CGRect = .zero) {
        super.init(frame: frame)
        setup()
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) { fatalError("init(coder:) has not been implemented") }
}

private extension ProfilePersonalInfoView {
    func setup() {
        avatarView.x.add(to: contentView).snp.makeConstraints {
            $0.width.height.equalTo(80)
            $0.centerY.equalToSuperview()
            $0.leading.equalTo(20)
        }

        userNameLabel.x.add(to: contentView).snp.makeConstraints {
            $0.leading.equalTo(avatarView.snp.trailing).offset(10)
            $0.bottom.equalTo(contentView.snp.centerY)
        }

        signatureLabel.x.add(to: contentView).snp.makeConstraints {
            $0.leading.equalTo(avatarView.snp.trailing).offset(10)
            $0.top.equalTo(contentView.snp.centerY).offset(4)
        }

        contentView.x.add(to: self).snp.makeConstraints {
            $0.top.leading.trailing.equalToSuperview()
            $0.bottom.equalTo(avatarView)
        }

        snp.updateConstraints {
            $0.height.equalTo(contentView)
        }

        let tapGesture = UITapGestureRecognizer()
        tapGesture.addTarget(self, action: #selector(contentViewClick(tap:)))
        contentView.addGestureRecognizer(tapGesture)
    }

    @objc
    func contentViewClick(tap _: UITapGestureRecognizer) {
        delegate?.infoViewDidSelected(self)
    }
}
